package com.telit.uav.controller;

import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.uav.service.UavQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @Auther zhouyu
 * @description 无人机视频接口
 **/
@RestController
@RequestMapping("/uav-video")
@Api(tags = "无人机视频接口",description = "/uav-video")
public class UavVideoController {

    @Value("${uav.h5.searchUrl}")
    private String searchUrl;

    @Autowired
    private UavQueryService uavQueryService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/queryHistoryVideo")
    @ApiOperation("app端调用 查询无人机历史视频")
    public DataReturn queryHistoryVideo(@RequestParam(required = true) String uavCode,
                                        @RequestParam(required = true) String startDate,
                                        @RequestParam(required = true) String endDate,
                                        @RequestParam(required = true) String session){
        try {
            String token = uavQueryService.queryTokenByUavCode(uavCode);
            if (token==null){
                return DataReturn.ok("该无人机不存在！");
            }
            Object object = restTemplate.getForObject(searchUrl + "?type=record&session="+session+"&start=" + startDate + "&end=" + endDate + "&token=" + token, Object.class);
            return DataReturn.ok(object);
        } catch (Exception e) {
            e.printStackTrace();
            return DataReturn.failure(ResultCode.OPERATE_ERROR,"查询失败！"+session);
        }
    }

}
